import { MockMethod } from 'vite-plugin-mock'

/**
 * if you use the real api, you should always insert the token from state into the api:header.
 */
export default [
  {
    url: '/api/login',
    method: 'get',
    response: ({ query }) => {
      return {
        code: 0,
        message: 'Login successfully',
        data: {
          userId: 0,
          name: 'Admin',
          mail: '666@google.com',
          token: `${query.username}&test&${query.password}`,
        }
      }
    },
  },
  {
    url: '/api/logOut',
    method: 'get',
    response: () => {
      // remove the token from the token pool;
      return {
        code: 0,
        message: 'Login out successfully',
      }
    },
  },
  {
    url: '/api/register',
    method: 'post',
    rawResponse: async (req, res) => {
      let reqbody = ''
      await new Promise((resolve) => {
        req.on('data', (chunk) => {
          reqbody += chunk
        })
        req.on('end', () => resolve(undefined))
      })
      res.setHeader('Content-Type', 'text/plain')
      res.statusCode = 200
      res.end(JSON.stringify({
        code: 0,
        message: 'Register successfully',
      }))
    },
  },
] as MockMethod[]